package com.haixiaoke.saas.repository.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.repository.exception.BizException;
import com.haixiaoke.saas.repository.mapper.*;
import com.haixiaoke.saas.repository.pojo.*;
import com.haixiaoke.saas.repository.service.IHouseInspectionService;
import com.haixiaoke.saas.repository.service.IOrderLockService;
import com.haixiaoke.saas.repository.service.IOrderOperatingRecordService;
import com.haixiaoke.saas.repository.vo.HouseInspectionVo;
import com.mzj.saas.commons.StatusCode;
import jodd.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 验房信息Service业务层处理
 *
 * @author haixiaoke
 * @date 2023-04-18
 */
@Service
public class HouseInspectionServiceImpl implements IHouseInspectionService {
    @Autowired
    private HouseInspectionMapper houseInspectionMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private HouseInspectionFacilityMapper houseInspectionFacilityMapper;
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private HouseQuitLeaseMapper houseQuitLeaseMapper;
    @Autowired
    private HousingAllocationMapper housingAllocationMapper;
    @Autowired
    private IOrderOperatingRecordService iOrderOperatingRecordService;
    @Autowired
    private IOrderLockService orderLockService;

    /**
     * 查询验房信息
     *
     * @param inspectionId 验房信息主键
     * @return 验房信息
     */
    @Override
    public HouseInspection selectHouseInspectionByInspectionId(Long inspectionId) {
        return houseInspectionMapper.selectHouseInspectionByInspectionId(inspectionId);
    }

    /**
     * 查询验房信息列表
     *
     * @param houseInspection 验房信息
     * @return 验房信息
     */
    @Override
    public List<HouseInspection> selectHouseInspectionList(HouseInspection houseInspection) {
        return houseInspectionMapper.selectHouseInspectionList(houseInspection);
    }

    /**
     * 新增用户入住验房信息
     *
     * @param houseInspectionVo 验房信息Vo
     * @return 结果
     */
    @Override
    public int inspectionOnCheckIn(HouseInspectionVo houseInspectionVo, String id) {
        String orderId = houseInspectionVo.getOrderId();
        Order order = orderMapper.selectOrderByOrderId(orderId);
        Integer orderState = order.getOrderState();

        //限制图片最多只能上传9张
        String[] split = houseInspectionVo.getHouseImg().split(",");
        if (split.length > 9) {
            throw new BizException(Integer.valueOf(StatusCode.ERROR_CODE_10004.getErrorCode()),"最多只能上传9张图片");
        }
        //用户入住时验房
        if (orderState == Order.ORDER_STATE_UN_INSPECTION) {
            HouseInspection houseInspection = new HouseInspection();
            Double housePrice = 0.0;
            Integer houseState = 1;
            BeanUtils.copyProperties(houseInspectionVo, houseInspection);
            houseInspection.setOrderId(order.getOrderId());
            houseInspection.setHouseId(order.getHouseId());
            houseInspection.setHousePrice(housePrice);
            houseInspection.setHouseState(houseState);
            houseInspection.setCreateBy(id);
            houseInspection.setCreateTime(new Date());
            houseInspection.setInspectionStatus(HouseInspection.ORDER_STATE_USER_HOUSE_INSPECTION);
            order.setOrderState(Order.ORDER_STATE_INSPECTION);
            orderMapper.updateOrder(order);
            //插入订单状态变更记录
            OrderOperatingRecord record = new OrderOperatingRecord();
            record.setOrderId(orderId);
            record.setOrderSourceState(Order.ORDER_STATE_UN_INSPECTION);
            record.setOrderState(Order.ORDER_STATE_INSPECTION);
            record.setRemark("用户已验房");
            record.setOperatingTime(new Date());
            record.setOperatingBy(id);
            record.setOperatingType(OrderOperatingRecord.OPERATING_TYPE_MANUAL);
            iOrderOperatingRecordService.insertOrderOperatingRecord(record);
            return houseInspectionMapper.insertHouseInspection(houseInspection);
        }
        return 0;
    }

    /**
     * 新增用户退租验房信息
     */
    @Override
    public int userInspectionOnCheckOut(HouseInspectionVo houseInspectionVo, String id) {
        String orderId = houseInspectionVo.getOrderId();
        HouseInspection houseInspection = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 1);

        //限制图片最多只能上传9张
        String[] split = houseInspectionVo.getHouseImg().split(",");
        if (split.length > 9) {
            throw new BizException(Integer.valueOf(StatusCode.ERROR_CODE_10004.getErrorCode()),"最多只能上传9张图片");
        }
        houseInspection.setHouseImg(houseInspectionVo.getHouseImg());
        HouseQuitLease houseQuitLease = houseQuitLeaseMapper.selectHouseQuitLeaseByOrderId(orderId);
        Integer quitLeaseStatus = houseQuitLease.getQuitLeaseStatus();
        //用户退租时验房
        if (quitLeaseStatus == HouseInspection.ORDER_STATE_USER_HOUSE_INSPECTION) {
            houseInspection.setCreateBy(id);
            houseInspection.setCreateTime(new Date());
            houseInspection.setInspectionStatus(HouseInspection.ORDER_STATE_USER_LEASE_HOUSE_INSPECTION);
            houseQuitLease.setQuitLeaseStatus(HouseQuitLease.QUIT_LEASE_STATUS_TENANT_CHECK);
            houseQuitLeaseMapper.updateHouseQuitLease(houseQuitLease);
            return houseInspectionMapper.insertHouseInspection(houseInspection);
        }
        return 0;
    }

    /**
     * 新增企业退租验房信息
     */
    @Override
    public int enterpriseInspectionOnCheckOut(HouseInspectionVo houseInspectionVo, String id) {

        String orderId = houseInspectionVo.getOrderId();
        Order order = orderMapper.selectOrderByOrderId(orderId);
        Integer orderState = order.getOrderState();
        String houseId = order.getHouseId();
        House house = houseMapper.selectHouseByIdAndStatus(houseId, 1);
        List<HouseInspectionFacility> houseInspectionFacilityList = houseInspectionVo.getHouseInspectionFacilityList();
        HouseInspection houseInspection = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 2);
        //商家退租时验房
        if (ObjectUtil.isEmpty(houseInspection)) {
           throw new BizException(Integer.valueOf(StatusCode.ERROR_CODE_10005.getErrorCode()),"需要租户验完房才能验房");
        }
        Double housePrice = houseInspection.getHousePrice();
        Integer houseState = houseInspection.getHouseState();
        houseInspection.setHouseImg(houseInspectionVo.getHouseImg());
        HouseQuitLease houseQuitLease = houseQuitLeaseMapper.selectHouseQuitLeaseByOrderId(orderId);
        Integer quitLeaseStatus = houseQuitLease.getQuitLeaseStatus();


        //商家验房将锁单表的锁单信息变更
        OrderLock orderLock = orderLockService.selectOrderLockByOrderId(orderId);
        orderLock.setCheckOutTime(order.getRealityCheckOutTime());
        orderLockService.updateOrderLockByOrderId(orderLock);


        //商家退租时验房
        if (quitLeaseStatus.equals(HouseQuitLease.QUIT_LEASE_STATUS_TENANT_CHECK) && orderState.equals(Order.ORDER_STATE_UN_CHECK_OUT)) {
            if (!houseInspectionFacilityList.isEmpty()) {
                for (HouseInspectionFacility houseInspectionFacility : houseInspectionFacilityList) {
                    Double facilitiesPrice = houseInspectionFacility.getFacilitiesPrice();
                    housePrice = facilitiesPrice + housePrice;
                    houseInspectionFacility.setInspectionStatus(HouseInspectionFacility.ORDER_STATE_ENTERPRISE_LEASE_HOUSE_INSPECTION);
                    houseInspectionFacility.setCreateBy("System");
                    houseInspectionFacility.setCreateTime(new Date());
                    houseInspectionFacilityMapper.updateHouseInspectionFacility(houseInspectionFacility);
                    //判断房屋设施是否有损坏
                    if (houseInspectionFacility.getFacilitiesState() == 2) {
                        houseState = 2;
                    }
                }
            }
            houseInspection.setCreateBy(id);
            houseInspection.setCreateTime(new Date());
            houseInspection.setHousePrice(housePrice);
            houseInspection.setHouseState(houseState);
            houseInspection.setInspectionStatus(HouseInspection.ORDER_STATE_ENTERPRISE_LEASE_HOUSE_INSPECTION);
            houseQuitLease.setQuitLeaseStatus(HouseQuitLease.QUIT_LEASE_STATUS_MERCHANT_CHECK);
            houseQuitLease.setInspectionResult(houseState);
            houseQuitLeaseMapper.updateHouseQuitLease(houseQuitLease);
            //退租成功修改出租状态
            house.setLeaseStatus(0L);
            houseMapper.updateHouse(house);
            //修改订单状态
            order.setOrderState(Order.ORDER_STATE_CHECK_OUT);
            orderMapper.updateOrder(order);
            //插入订单状态变更记录
            OrderOperatingRecord record = new OrderOperatingRecord();
            record.setOrderId(orderId);
            record.setOrderSourceState(Order.ORDER_STATE_UN_CHECK_OUT);
            record.setOrderState(Order.ORDER_STATE_CHECK_OUT);
            record.setRemark("用户已退租");
            record.setOperatingTime(new Date());
            record.setOperatingBy(id);
            record.setOperatingType(OrderOperatingRecord.OPERATING_TYPE_MANUAL);
            iOrderOperatingRecordService.insertOrderOperatingRecord(record);
            return houseInspectionMapper.insertHouseInspection(houseInspection);
        }
        return 0;
    }

    @Override
    public List<HouseInspection> selectHouseInspectionByOrderId(String orderId) {
        return houseInspectionMapper.selectHouseInspectionByOrderId(orderId);
    }

    @Override
    public HouseInspection selectHouseInspectionByOrderIdAndInspectionStatus(String orderId, Integer inspectionStatus) {
        return houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, inspectionStatus);
    }

    /**
     * 修改验房信息
     *
     * @param houseInspection 验房信息
     * @return 结果
     */
    @Override
    public int updateHouseInspection(HouseInspection houseInspection) {
        houseInspection.setUpdateTime(new Date());
        return houseInspectionMapper.updateHouseInspection(houseInspection);
    }

    @Override
    public int insertHouseInspection(HouseInspection houseInspection) {
        return houseInspectionMapper.insertHouseInspection(houseInspection);
    }

    /**
     * 批量删除验房信息
     *
     * @param inspectionIds 需要删除的验房信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseInspectionByInspectionIds(Long[] inspectionIds) {
        return houseInspectionMapper.deleteHouseInspectionByInspectionIds(inspectionIds);
    }

    /**
     * 删除验房信息信息
     *
     * @param inspectionId 验房信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseInspectionByInspectionId(Long inspectionId) {
        return houseInspectionMapper.deleteHouseInspectionByInspectionId(inspectionId);
    }

    /**
     * 查询验房信息
     */
    @Override
    public HouseInspectionVo selectHouseInspection(HouseInspection houseInspection) {
        String orderId = houseInspection.getOrderId();
        houseInspection = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 1);
        HouseInspectionVo houseInspectionVo = new HouseInspectionVo();
        //String houseImg = null;
        if (ObjectUtil.isNotEmpty(houseInspection)) {
            //houseImg = houseInspection.getHouseImg();
            houseInspectionVo.setIntakeHouseImg(houseInspection.getHouseImg());
        }
        HouseInspection houseInspectionItem = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 2);
        if (ObjectUtil.isNotEmpty(houseInspectionItem)) {
          /*  if (ObjectUtil.isNotEmpty(houseImg)) {
                String img = houseInspectionItem.getHouseImg();
                if (ObjectUtil.isNotEmpty(img)) {
                    houseImg = houseImg + "," + img;
                }
            } else {
                String img = houseInspectionItem.getHouseImg();
                if (ObjectUtil.isNotEmpty(img)) {
                    houseImg = img;
                }
            }*/
            houseInspectionVo.setLeaseHouseImg(houseInspectionItem.getHouseImg());
        }
        Order order = orderMapper.selectOrderByOrderId(orderId);
        houseInspectionVo.setInspectionId(houseInspection.getInspectionId());
        houseInspectionVo.setHouseId(houseInspection.getHouseId());
        houseInspectionVo.setOrderId(orderId);
        //houseInspectionVo.setHouseImg(houseImg);
        houseInspectionVo.setHouseState(1);
        houseInspectionVo.setHousePrice(0.0);
        HouseInspectionFacility houseInspectionFacility = new HouseInspectionFacility();
        houseInspectionFacility.setOrderId(orderId);
        houseInspectionVo.setCleaningType(order.getCleaningType());
        List<HouseInspectionFacility> houseInspectionFacilities = houseInspectionFacilityMapper.selectHouseInspectionFacilityList(houseInspectionFacility);
        houseInspectionVo.setHouseInspectionFacilityList(houseInspectionFacilities);
        return houseInspectionVo;
    }

    @Override
    public Order selectOrderByOrderId(String orderId) {
        return orderMapper.selectOrderByOrderId(orderId);
    }

    @Override
    public House selectHouseByIdAndStatus(String houseId, int i) {
        return houseMapper.selectHouseByIdAndStatus(houseId, i);
    }

    @Override
    public HouseQuitLease selectHouseQuitLeaseByOrderId(String orderId) {
        return houseQuitLeaseMapper.selectHouseQuitLeaseByOrderId(orderId);
    }

    @Override
    public HouseInspectionVo selectHouseInspectionCleaning(HouseInspection houseInspection, Integer loginType) {
        String orderId = houseInspection.getOrderId();
        HouseInspectionVo houseInspectionVo = new HouseInspectionVo();
        Integer cleaningType = null;
        Double cleaningPrice = null;
        HouseInspectionFacility houseInspectionFacility = new HouseInspectionFacility();
        if (loginType == 1) {
            HouseInspection houseInspectionItem = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 3);
            if (ObjectUtil.isNotEmpty(houseInspectionItem)) {
                BeanUtils.copyProperties(houseInspectionItem, houseInspectionVo);
                String houseId = houseInspectionItem.getHouseId();
                Order order = orderMapper.selectOrderByOrderId(orderId);
                cleaningType = order.getCleaningType();
                HousingAllocation housingAllocation = housingAllocationMapper.selectHousingAllocationByHouseId(houseId);
                if (ObjectUtil.isNotEmpty(housingAllocation)) {
                    cleaningPrice = housingAllocation.getCleaningPrice();
                }
            }
            HouseInspection userHouseInspection = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 1);
            String houseImg = "";
            if (ObjectUtil.isNotEmpty(userHouseInspection)) {
                houseImg = houseInspection.getHouseImg();
            }
            HouseInspection userHouseInspection1 = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 2);
            if (ObjectUtil.isNotEmpty(houseImg)) {
                String img = userHouseInspection1.getHouseImg();
                if (ObjectUtil.isNotEmpty(img)) {
                    houseImg = houseImg + "," + img;
                }
            } else {
                String img = userHouseInspection1.getHouseImg();
                if (ObjectUtil.isNotEmpty(img)) {
                    houseImg = img;
                }
            }
            houseInspectionVo.setUserHouseImg(houseImg);
            houseInspectionFacility.setOrderId(orderId);
            List<HouseInspectionFacility> houseInspectionFacilities = houseInspectionFacilityMapper.selectHouseInspectionFacilityList(houseInspectionFacility);
            houseInspectionVo.setHouseInspectionFacilityList(houseInspectionFacilities);
            houseInspectionVo.setCleaningType(cleaningType);
            houseInspectionVo.setCleaningPrice(cleaningPrice);
            return houseInspectionVo;
        } else {
            HouseInspection houseInspectionItem = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 2);
            if (ObjectUtil.isNotEmpty(houseInspectionItem)) {
                BeanUtils.copyProperties(houseInspectionItem, houseInspectionVo);
                String houseId = houseInspectionItem.getHouseId();
                Order order = orderMapper.selectOrderByOrderId(orderId);
                cleaningType = order.getCleaningType();
                HousingAllocation housingAllocation = housingAllocationMapper.selectHousingAllocationByHouseId(houseId);
                if (ObjectUtil.isNotEmpty(housingAllocation)) {
                    cleaningPrice = housingAllocation.getCleaningPrice();
                }
            }
            HouseInspection userHouseInspection = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId, 1);
            String houseImg = "";
            if (ObjectUtil.isNotEmpty(userHouseInspection)) {
                if (ObjectUtil.isEmpty(houseInspectionItem)) {
                    BeanUtils.copyProperties(userHouseInspection, houseInspectionVo);
                }
                houseImg = houseInspection.getHouseImg();
            }
            if (ObjectUtil.isNotEmpty(houseImg)) {
                String img = houseInspectionItem.getHouseImg();
                if (ObjectUtil.isNotEmpty(img)) {
                    houseImg = houseImg + "," + img;
                }
            } else if (ObjectUtil.isNotEmpty(houseInspectionItem)) {
                if (StringUtil.isNotEmpty(houseInspectionItem.getHouseImg())) {
                    String img = houseInspectionItem.getHouseImg();
                    if (ObjectUtil.isNotEmpty(img)) {
                        houseImg = img;
                    }
                }
            }
            houseInspectionVo.setUserHouseImg(houseImg);
            //List<HouseInspection> houseInspectionList = new ArrayList<>();
            //HouseInspection item = houseInspectionMapper.selectHouseInspectionByOrderIdAndInspectionStatus(orderId,1);
            //houseInspectionList.add(item);
            //houseInspectionList.add(houseInspectionItem);
            houseInspectionFacility.setOrderId(orderId);
            //houseInspectionFacility.setInspectionStatus(1);
            List<HouseInspectionFacility> houseInspectionFacilities = houseInspectionFacilityMapper.selectHouseInspectionFacilityList(houseInspectionFacility);
            houseInspectionVo.setHouseInspectionFacilityList(houseInspectionFacilities);
            houseInspectionVo.setCleaningType(cleaningType);
            houseInspectionVo.setCleaningPrice(cleaningPrice);
            return houseInspectionVo;
        }

    }


    @Override
    public HouseInspectionVo selectHouseInspectionListByOrderId(String orderId) {
        HouseInspectionVo vo = new HouseInspectionVo();
        List<HouseInspection> houseInspections = houseInspectionMapper.selectHouseInspectionByOrderId(orderId);
        List<HouseInspectionFacility> houseInspectionFacilities = houseInspectionFacilityMapper.selectHouseInspectionFacilityByOrderId(orderId);
        Order order = orderMapper.selectOrderByOrderId(orderId);
        vo.setHouseInspectionList(houseInspections);
        vo.setHouseInspectionFacilityList(houseInspectionFacilities);
        vo.setOrder(order);
        return vo;
    }

}
